package com.example.order.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.example.order.R;
import com.example.order.activity.TestActivity;

import java.util.List;

public class TestAdapter extends BaseAdapter implements View.OnClickListener {


    private List<TestActivity.Data> mContentList;
    private LayoutInflater mInflater;
    private Callback mCallback;

    public TestAdapter(List<TestActivity.Data> contentList, LayoutInflater mInflater, Callback mCallback) {
        this.mContentList = contentList;
        this.mInflater = mInflater;
        this.mCallback = mCallback;
    }
    public TestAdapter(List<TestActivity.Data> contentList, Context context, Callback mCallback) {
        this.mContentList = contentList;
        this.mInflater =LayoutInflater.from(context);
        this.mCallback = mCallback;
    }

    public interface Callback{
        public void click(View v);
    }
    @Override
    public int getCount() {
        return mContentList.size();
    }

    @Override
    public Object getItem(int position) {
        return mContentList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    //ListView的内部类
    public class ViewHolder{
        private TextView text1;
        private TextView text2;
        private Button button1;
        private Button button2;

        public TextView getText1() {
            return text1;
        }

        public void setText1(TextView text1) {
            this.text1 = text1;
        }

        public TextView getText2() {
            return text2;
        }

        public void setText2(TextView text2) {
            this.text2 = text2;
        }

        public Button getButton1() {
            return button1;
        }

        public void setButton1(Button button1) {
            this.button1 = button1;
        }

        public Button getButton2() {
            return button2;
        }

        public void setButton2(Button button2) {
            this.button2 = button2;
        }
    }

    @SuppressLint({"InflateParams", "ViewHolder"})
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
       /* if(convertView==null)
        {
            convertView=mInflater.inflate(R.layout.test_adapter,null);
            holder=new ViewHolder();
            TextView text1=(TextView) convertView.findViewById(R.id.test_text1);
            TextView text2=(TextView) convertView.findViewById(R.id.test_text2);
            Button button1=(Button)convertView.findViewById(R.id.test_button1);
            Button button2=(Button)convertView.findViewById(R.id.test_button2);
            button1.setText("+1");
            button2.setText("-1");
            text1.setText(mContentList.get(position).getText1());
            text2.setText(mContentList.get(position).getText2());
            button1.setOnClickListener(this);
            button2.setOnClickListener(this);
            holder.setText1(text1);
            holder.setText2(text2);
            holder.setButton1(button1);
            holder.setButton2(button2);
            //return convertView;
        }*/
        convertView=mInflater.inflate(R.layout.test_adapter,null);
        holder=new ViewHolder();
        TextView text1=(TextView) convertView.findViewById(R.id.test_text1);
        TextView text2=(TextView) convertView.findViewById(R.id.test_text2);
        Button button1=(Button)convertView.findViewById(R.id.test_button1);
        Button button2=(Button)convertView.findViewById(R.id.test_button2);
        button1.setText("+1");
        button2.setText("-1");
        text1.setText(mContentList.get(position).getText1());
        text2.setText(mContentList.get(position).getText2());
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        holder.setText1(text1);
        holder.setText2(text2);
        holder.setButton1(button1);
        holder.setButton2(button2);
        return convertView;
    }

    @Override
    public void onClick(View v) {
    mCallback.click(v);
    }
}
